iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 21
0

》Jump mechanics
今日我們要來實行跳躍動作以及限制遊戲範圍。例如像馬力歐可能就是 X 軸移動,小朋友下樓梯就是 Y 軸移動,那我們就需要設定遊戲的範圍大小。

》Javascript 內容
先來處理遊戲界線範圍

scene.create = function() {
    .....
    this.player.body.setCollideWorldBounds(true)
    .....
}

我們也可以設定遊戲的範圍大小,不管是要 X 軸來執行遊戲,還是Y軸來執行遊戲,都可以設定遊戲範圍的大小

this.physics.world.bounds.width = 360
this.physics.world.bounds.height = 1000

接著多處理按空白鍵,會讓 player 進行跳躍的動作

scene.update = function() {
    if (this.cursors.space.isDown) {
        this.player.body.setVelocityY(-200)
    }
}

現在有存在一個問題,當一直按 space 時,player 會一直跳躍,但這不是我們要的,是不能讓 player 持續在空中進行跳躍,所以要多判別是否觸地,再進行跳躍的動作。

if (this.player.body.blocked.down && this.cursors.space.isDown) {
    this.player.body.setVelocityY(-500)
}

》結論
今日我們練習到了如何限制遊戲範圍,以及跳躍的動作的設定。當中也許有不完善的地方,大家可以試著調整看看,例如 sprite 是否應該要多增加一個跳躍的動作?在執行跳躍的動作是否不能執行左右鍵的動畫?這些都是可以再加強的地方,可以試試看!
https://ithelp.ithome.com.tw/upload/images/20181105/20111617ttxRPoljHk.png


今天就先到這裡,我們明天見。


上一篇
Day 20:Walking mechanics
下一篇
Day 22:JSON
系列文
Phaser 幫我撐個 30 天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言